package com.amap.search.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.amap.search.entity.db2022.AddressCode;
import com.amap.search.entity.excel.AdCodeExcel;
import com.amap.search.mapper.db2022.AddressCodeMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class AdCodeExcelListener extends AnalysisEventListener<AdCodeExcel> {

    @Resource
    AddressCodeMapper addressCodeMapper;

    private Map<String, String> areaNameMap = new HashMap<>();

    List<AddressCode> addressCodeList = new ArrayList<>();

    public AdCodeExcelListener() {
    }

    @Override
    public void invoke(AdCodeExcel adCodeExcel, AnalysisContext analysisContext) {
        AddressCode addressCode = new AddressCode();
        String code = adCodeExcel.getAdCode();
        // 国家号不处理
        if ("00000".equals(code.substring(1))) {
            return;
        }
        if (isProvinceCode(code) || isCityCode(code)) {
            areaNameMap.put(adCodeExcel.getAdCode(), adCodeExcel.getName());
            transferAddress(addressCode, adCodeExcel);
        }
        transferAddress(addressCode, adCodeExcel);
        addressCodeList.add(addressCode);
        if (addressCodeList.size() == 50) {
            addressCodeMapper.insertBatch(addressCodeList);
            addressCodeList.clear();
        }
    }

    private boolean isProvinceCode(String code) {
        return "0000".equals(code.substring(code.length() - 4));
    }

    private boolean isCityCode(String code) {
        return "00".equals(code.substring(code.length() - 2));
    }

    private void transferAddress(AddressCode addressCode, AdCodeExcel excel) {
        String adCode = excel.getAdCode();
        String cityCode = getSuffixZero(adCode, 2);
        String provinceCode = getSuffixZero(adCode, 4);
        addressCode.setAdCode(excel.getAdCode());
        addressCode.setCityCode(cityCode);
        addressCode.setProvinceCode(provinceCode);
        addressCode.setAdName(excel.getName());
        addressCode.setCityName(areaNameMap.get(cityCode) == null ? "" : areaNameMap.get(cityCode));
        addressCode.setProvinceName(areaNameMap.get(provinceCode) == null ? "" : areaNameMap.get(provinceCode));
        // 清空地区名
        if (adCode.equals(cityCode)) {
            addressCode.setAdName("");
        }
        // 清空城市名
        if (adCode.equals(provinceCode)) {
            addressCode.setCityName("");
        }
    }

    private String getSuffixZero(String code, int len) {
        char[] chars = code.toCharArray();
        int n = chars.length - 1;
        for (int i = 0; i < len; i++) {
            chars[n - i] = '0';
        }
        return new String(chars);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (CollectionUtils.isNotEmpty(addressCodeList)) {
            addressCodeMapper.insertBatch(addressCodeList);
        }
        log.info("excel导入完毕");
    }
}
